package cn.yl.order.server.controller;


import cn.yl.api.server.result.Result;
import cn.yl.api.server.util.JwtUtil;
import cn.yl.order.server.config.WechatPayConfig;
import cn.yl.order.server.entity.PayOrder;
import cn.yl.order.server.entity.UserCourseOrder;
import cn.yl.order.server.entity.vo.PayOrderVo;

import cn.yl.order.server.service.PayOrderService;
import cn.yl.order.server.service.UserCourseOrderService;
import cn.yl.order.server.service.feign.CourseFeignService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayUtil;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.jfinal.kit.HttpKit;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * <p>
 * 用户课程订单表 前端控制器
 * </p>
 *
 * @author yl
 * @since 2022-04-22
 */

@RestController
@RequestMapping("/order")
@Slf4j
public class UserCourseOrderController {

    private final PayOrderService payOrderService;

    private final UserCourseOrderService userCourseOrderService;

    private final WechatPayConfig wechatPayConfig;

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private CourseFeignService courseFeignService;

    public UserCourseOrderController(WechatPayConfig wechatPayConfig, UserCourseOrderService userCourseOrderService, PayOrderService payOrderService) {
        this.wechatPayConfig = wechatPayConfig;
        this.userCourseOrderService = userCourseOrderService;
        this.payOrderService = payOrderService;
    }

    @GetMapping("getOKOrderCourseIds")
    public Result getOKOrderCourseIds(String jwt) {
        ArrayList<Integer> ids = new ArrayList<>();
        List<UserCourseOrder> okOrderCourseIds = userCourseOrderService.getOKOrderCourseIds(jwt);
        okOrderCourseIds.forEach(userCourseOrder -> {
            ids.add(userCourseOrder.getCourseId());
        });
        return Result.ok().setData("items", ids);
    }

    @GetMapping("getOKOrderCourseIDS")
    public Result getOKOrderCourseIds(HttpServletRequest request) {
        String jwt = request.getHeader("jwt");
        ArrayList<Integer> ids = new ArrayList<>();
        List<UserCourseOrder> okOrderCourseIds = userCourseOrderService.getOKOrderCourseIds(jwt);
        okOrderCourseIds.forEach(userCourseOrder -> {
            ids.add(userCourseOrder.getCourseId());
        });
        return Result.ok().setData("items", ids);
    }

    @GetMapping("getMyCourseOrderById")
    public Map<String, String> getMyCourseOrderById(Integer userId, Integer courseId, Integer sourceType) {
        QueryWrapper<UserCourseOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId).eq("course_id", courseId).eq("source_type", sourceType);
        UserCourseOrder userCourseOrder = this.userCourseOrderService.getOne(wrapper);
        HashMap<String, String> map = new HashMap<>();
        if (userCourseOrder == null) {
            return null;
        }
        map.put("status", userCourseOrder.getStatus().toString());
        map.put("codeUrl", userCourseOrder.getCodeUrl());
        map.put("orderId", userCourseOrder.getOrderNo());
        return map;
    }

    @GetMapping("timeOutOrderIdList/{time}")
    public List<String> timeOutOrderIdList(@PathVariable Integer time) {
        List<String> timeOutOrderIdList = this.userCourseOrderService.getTimeOutOrder(time);
        return timeOutOrderIdList;
    }
}

